package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.CouponInfoMapper;
import cc.rengu.igas.momp.common.dao.McDetailInfoMapper;
import cc.rengu.igas.momp.common.dao.McUserInfoMapper;
import cc.rengu.igas.momp.common.dao.impl.CouponInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.McDetailInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.McUserInfoMapperImpl;
import cc.rengu.igas.momp.common.entity.TMCouponInfo;
import cc.rengu.igas.momp.common.entity.TMMcCouponInfo;
import cc.rengu.igas.momp.common.entity.TMMcUserInfo;
import cc.rengu.igas.momp.common.enums.CouponStatusEnum;
import cc.rengu.igas.momp.common.enums.McStatusEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.common.util.MompDateUtil;
import cc.rengu.igas.momp.facade.request.ClaimCouponOutChannelRequest;
import cc.rengu.igas.momp.facade.response.ClaimCouponOutChannelResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.BinInfo;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.BinInfoService;
import cc.rengu.oltp.service.realize.impl.BinInfoServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.dbs.Database;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 外部渠道卡券领取功能实现
 *
 * @author liujinan
 * @since 2021/1/14 16:59
 */
public class ClaimCouponServiceOutChannel extends RadpService {

    private String globalInstId;
    private String globalTimeStamp;
    private String globalTxnDate;
    private String globalTxnTime;
    private String globalMcNo;
    private String globalRuleIndex;
    private String globalClaimUser;
    private String globalClaimType;
    private String globalPurchaseAmt;
    private String globalCouponLockFlag;
    private String globalChannelId;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {
            ClaimCouponOutChannelRequest claimCouponOutChannelRequest = new ClaimCouponOutChannelRequest();
            ConvertUtil.convertOutput(claimCouponOutChannelRequest);

            ClaimCouponOutChannelResponse claimCouponOutChannelResponse = new ClaimCouponOutChannelResponse();
            claimCouponOutChannelResponse.setHeader(claimCouponOutChannelRequest.getHeader());
            BizResponse<ClaimCouponOutChannelResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(claimCouponOutChannelResponse);

            /* 请求报文检查 */
            messageValidation(claimCouponOutChannelRequest);

            // 服务调用
            BizResponse<ClaimCouponOutChannelResponse> bizResponseNew = claimCoupon(claimCouponOutChannelRequest, claimCouponOutChannelResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 领取卡券流程
     *
     * @param claimCouponOutChannelRequest  领取卡券接口请求对象
     * @param claimCouponOutChannelResponse 领取卡券接口应答对象
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2021/1/14 16:59
     */
    private BizResponse<ClaimCouponOutChannelResponse> claimCoupon(ClaimCouponOutChannelRequest claimCouponOutChannelRequest, ClaimCouponOutChannelResponse claimCouponOutChannelResponse) throws Exception {

        BizResponse<ClaimCouponOutChannelResponse> bizResponse = new BizResponse<>();

        //用户领取卡券
        claimSingleCoupon(claimCouponOutChannelRequest, claimCouponOutChannelResponse);

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        claimCouponOutChannelResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        claimCouponOutChannelResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(claimCouponOutChannelResponse);

        return bizResponse;
    }

    /**
     * 领取单张卡券
     *
     * @param claimCouponOutChannelResponse 领取卡券接口响应对象
     * @author liujinan
     * @since 2021/1/14 16:59
     */
    private void claimSingleCoupon(ClaimCouponOutChannelRequest claimCouponOutChannelRequest, ClaimCouponOutChannelResponse claimCouponOutChannelResponse) throws Exception {

        /* 判断类型，如果是免费型的卡券直接领取卡券*/
        if (null == globalPurchaseAmt || CommonConstant.ZERO_AMOUNT.equals(globalPurchaseAmt) || "".equals(globalPurchaseAmt)) {
            rglog.info("营销活动编号<{}>，规则编号<{}> 用户<{}> 免费型卡券领取开始...", globalMcNo, globalRuleIndex, globalClaimUser);
            //领取免费型卡券
            TMCouponInfo tMCouponInfo = claimSingleCouponForFree();
            //卡券信息转换
            convertCouponInfo(tMCouponInfo, claimCouponOutChannelRequest, claimCouponOutChannelResponse);
            rglog.info("营销活动编号<{}>，规则编号<{}> 用户<{}> 领取卡券成功，卡券编号<{}>", globalMcNo, globalRuleIndex, globalClaimUser, tMCouponInfo.getCouponNo());

        }
    }


    /**
     * 领取免费型卡券
     *
     * @author liujinan
     * @since 2021/1/14 16:59
     */
    private TMCouponInfo claimSingleCouponForFree() throws Exception {

        //查询营销活动信息
        TMMcCouponInfo mcCouponInfo = selectCouponMcInfoByPrimaryKey(globalInstId, globalMcNo);
        if (null != mcCouponInfo) {
            /* 检查卡券营销活动是否指定领取渠道，如果指定，判断当前上送的渠道标识是否匹配*/
            if (!globalChannelId.equals(mcCouponInfo.getClaimChannelId())) {
                /* 营销活动不在进行中,不可领取卡券 */
                rglog.error("营销活动编号<{}> ,活动指定领取渠道<{}> 当前领取上送的渠道<{}> 不可领取卡券", globalMcNo, mcCouponInfo.getClaimChannelId(), globalChannelId);
                throw new BizException(RespCodeEnum.FAILED_TO_CLIAM_COUPON_INFO_ERROR.getRespCode(), RespCodeEnum.FAILED_TO_CLIAM_COUPON_INFO_ERROR.getRespDesc());
            }

            /* 检查卡券营销活动状态*/
            if (!McStatusEnum.PROGRESSING.getMcStatusCode().equals(mcCouponInfo.getMcStatus())) {
                /* 营销活动不在进行中,不可领取卡券 */
                rglog.error("营销活动编号<{}> ,营销活动状态不是进行中，不可领取卡券", globalMcNo);
                throw new BizException(RespCodeEnum.FAILED_TO_CLIAM_COUPON_INFO_ERROR.getRespCode(), RespCodeEnum.FAILED_TO_CLIAM_COUPON_INFO_ERROR.getRespDesc());
            }

            /* 检查卡券营销活动客户范围 */
            if (CommonConstant.ONE_COMMON_CONSTANT.equals(mcCouponInfo.getClientRange())) {
                //查询营销活动用户信息表信息
                List<TMMcUserInfo> userInfoList = checkUserInfo(globalInstId, globalMcNo, globalClaimUser);
                if (null == userInfoList) {
                    rglog.error("营销活动编号<{}> ,营销活动为指定用户参加，当前领取人<{}> 不在指定用户中，不可领取卡券", globalMcNo, globalClaimUser);
                    throw new BizException(RespCodeEnum.FAILED_TO_CLIAM_COUPON_INFO_ERROR.getRespCode(), RespCodeEnum.FAILED_TO_CLIAM_COUPON_INFO_ERROR.getRespDesc());
                }
            }

            /* 检查卡券营销活动是否是免费型卡券营销活动 */
            if (CommonConstant.ONE_COMMON_CONSTANT.equals(mcCouponInfo.getPurchaseFlag())) {
                /* 营销活动卡券为购买型 */
                rglog.error("营销活动编号<{}>,<{}>", globalMcNo, RespCodeEnum.WRONG_MC_PURCHASE_FLAG_ERROR.getRespDesc());
                throw new BizException(RespCodeEnum.FAILED_TO_CLIAM_COUPON_INFO_ERROR.getRespCode(), RespCodeEnum.FAILED_TO_CLIAM_COUPON_INFO_ERROR.getRespDesc());
            }

            /* 检查当前系统日期是否在对应卡券营销活动的有效期内 */
            int returnCode = MompDateUtil.checkDateRange(globalTxnDate, mcCouponInfo.getMcStartDate(), mcCouponInfo.getMcEndDate());
            if (CommonConstant.PROCESS_SUCCESS != returnCode) {
                rglog.error("领取卡券日期<{}>并未在卡券营销活动<{}>的有效期<{}>~<{}>内,不可领取!", globalTxnDate, globalMcNo, mcCouponInfo.getMcStartDate(), mcCouponInfo.getMcEndDate());
                throw new BizException(RespCodeEnum.FAILED_TO_CLIAM_COUPON_INFO_ERROR.getRespCode(), RespCodeEnum.FAILED_TO_CLIAM_COUPON_INFO_ERROR.getRespDesc());
            }

            /* 查询卡券信息表中剩余可领取卡券信息 */
            TMCouponInfo couponInfo = new TMCouponInfo();
            couponInfo.setInstId(globalInstId);
            couponInfo.setMcNo(globalMcNo);
            couponInfo.setRuleIndex(globalRuleIndex);

            /* 查询未生效或未领取的卡券状态的卡券 */
            TMCouponInfo tmCouponInfo = mompSelectClaimableFreeCoupon(couponInfo, "");
            if (null == tmCouponInfo) {
                rglog.error("法人机构<{}>的卡券营销活动<{}>的规则<{}>没有剩余可领取卡券!", globalInstId, globalMcNo, globalRuleIndex);
                throw new BizException(RespCodeEnum.FAILED_TO_CLIAM_COUPON_INFO_ERROR.getRespCode(), RespCodeEnum.FAILED_TO_CLIAM_COUPON_INFO_ERROR.getRespDesc());
            }

            /* 更新卡券状态 */
            couponInfo.setCouponNo(tmCouponInfo.getCouponNo());
            couponInfo.setClaimUserId(globalClaimUser);
            couponInfo.setClaimDate(globalTxnDate);
            couponInfo.setClaimTime(globalTxnTime);
            couponInfo.setClaimType(globalClaimType);
            couponInfo.setUpdateTime(globalTimeStamp);

            /* 根据卡券生效时间更新卡券状态 */
            if (CommonConstant.COUPON_IMMEDIATE_AVAILABLE.equals(mcCouponInfo.getAvailableDate())) {
                /* 卡券实时生效,需要将卡券状态更新为未核销 */
                couponInfo.setCouponStatus(CouponStatusEnum.NOT_REDEEMED.getCouponStatusCode());
                tmCouponInfo.setCouponStatus(CouponStatusEnum.NOT_REDEEMED.getCouponStatusCode());
            } else {
                /* 卡券第二天生效,需要将卡券状态更新为未生效 */
                couponInfo.setCouponStatus(CouponStatusEnum.NOT_ENABLED.getCouponStatusCode());
                tmCouponInfo.setCouponStatus(CouponStatusEnum.NOT_ENABLED.getCouponStatusCode());
            }

            /* 根据卡券有效期生成卡券有效期起止日期 */
            if (CommonConstant.COUPON_RELATIVE_DATE.equals(mcCouponInfo.getCouponDateFlag())) {
                /* 计算卡券有效起止日期并更新 */
                calculateCouponAvailableDateImmediate(couponInfo, mcCouponInfo.getCouponPeriod());
                tmCouponInfo.setCouponStartDate(couponInfo.getCouponStartDate());
                tmCouponInfo.setCouponEndDate(couponInfo.getCouponEndDate());
            } else {
                /* 不需要更新卡券有效期止日期,有效起止日期仍为查询出来的日期更新卡券状态 */
                couponInfo.setCouponStartDate(tmCouponInfo.getCouponStartDate());
                couponInfo.setCouponEndDate(tmCouponInfo.getCouponEndDate());
            }

            /* 更新卡券状态 */
            returnCode = updateCouponInfoAndDate(couponInfo);
            if (Database.DBS_SUCCESS != returnCode) {
                rglog.error("更新卡券状态失败! RETURN_CODE=<{}>", returnCode);
                throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }

            /* 转换卡券信息 */
            tmCouponInfo.setCouponStatus(couponInfo.getCouponStatus());
            tmCouponInfo.setClaimUserId(globalClaimUser);
            tmCouponInfo.setClaimDate(globalTxnDate);
            tmCouponInfo.setClaimTime(globalTxnTime);
            tmCouponInfo.setClaimType(globalClaimType);

            return tmCouponInfo;
        } else {
            /* 获取卡券营销活动信息失败 */
            rglog.error("机构<{}>, 营销活动编号<{}>,<{}>", globalInstId, globalMcNo, RespCodeEnum.GET_COUPON_MC_DETAIL_ERROR.getRespDesc());
            throw new BizException(RespCodeEnum.GET_COUPON_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_COUPON_MC_DETAIL_ERROR.getRespDesc());
        }
    }

    /**
     * 查询卡券营销活动基本信息
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return cc.rengu.igas.momp.common.entity.TMMcCouponInfo 卡券类营销活动信息表
     * @author liujinan
     * @since 2021/1/14 16:59
     */
    private TMMcCouponInfo selectCouponMcInfoByPrimaryKey(String instId, String mcNo) throws Exception {

        /* 营销活动信息正式表查询 */
        TMMcCouponInfo mcCouponInfo = new TMMcCouponInfo();
        mcCouponInfo.setInstId(instId);
        mcCouponInfo.setMcNo(mcNo);

        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.selectCouponMcInfoByPrimaryKey(mcCouponInfo);
    }

    /**
     * 数据库实体类和接口实体类数据转换
     *
     * @param couponInfo 卡券信息
     * @return void
     * @author liujinan
     * @since 2021/1/14 16:59
     */
    private void convertCouponInfo(TMCouponInfo couponInfo, ClaimCouponOutChannelRequest claimCouponOutChannelRequest, ClaimCouponOutChannelResponse claimCouponOutChannelResponse) {

        /* 营销活动编号 */
        claimCouponOutChannelResponse.setMcNo(couponInfo.getMcNo());
        /* 规则编号 */
        claimCouponOutChannelResponse.setRuleIndex(couponInfo.getRuleIndex());
        /* 领取人 */
        claimCouponOutChannelResponse.setClaimUser(couponInfo.getClaimUserId());
        /* 卡券编号 */
        claimCouponOutChannelResponse.setCouponNo(couponInfo.getCouponNo());
        /* 卡券有效起始日期 */
        claimCouponOutChannelResponse.setCouponStartDate(couponInfo.getCouponStartDate());
        /* 卡券有效结束日期 */
        claimCouponOutChannelResponse.setCouponEndDate(couponInfo.getCouponEndDate());
        /* 领取日期 */
        claimCouponOutChannelResponse.setClaimDate(couponInfo.getClaimDate());
        /* 领取时间 */
        claimCouponOutChannelResponse.setClaimTime(couponInfo.getClaimTime());
        /* 卡券状态 */
        claimCouponOutChannelResponse.setCouponStatus(couponInfo.getCouponStatus());
        /* 卡券可抵扣金额 */
        claimCouponOutChannelResponse.setCouponAmt(couponInfo.getCouponAmt());
        /* 卡券购买金额 */
        claimCouponOutChannelResponse.setPurchaseAmt(claimCouponOutChannelRequest.getPurchaseAmt());

        return;
    }

    /**
     * 计算卡券有效期
     *
     * @param tmCouponInfo 卡券信息表
     * @author liujinan
     * @since 2021/1/14 16:59
     */
    private void calculateCouponAvailableDateImmediate(TMCouponInfo tmCouponInfo, String couponPeriod) {

        String startDate = globalTxnDate;
        if (couponPeriod.equals(CommonConstant.ONE_COMMON_CONSTANT)) {
            String endDate = DateUtil.getBaseDateOffsetDay(globalTxnDate, Integer.parseInt(couponPeriod));

            tmCouponInfo.setCouponStartDate(endDate);
            tmCouponInfo.setCouponEndDate(endDate);
        } else {
            String endDate = DateUtil.getBaseDateOffsetDay(globalTxnDate, Integer.parseInt(couponPeriod) - 1);

            tmCouponInfo.setCouponStartDate(startDate);
            tmCouponInfo.setCouponEndDate(endDate);
        }
    }

    /**
     * 领取免费型卡券更新卡券相关信息和卡券有效期
     *
     * @param tmCouponInfo 卡券信息
     * @return int
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2021/1/14 16:59
     */
    private int updateCouponInfoAndDate(TMCouponInfo tmCouponInfo) throws Exception {

        CouponInfoMapper couponInfoMapper = new CouponInfoMapperImpl();
        return couponInfoMapper.updateCouponInfoAndDate(tmCouponInfo);
    }

    /**
     * 查询可领取的免费卡券信息
     *
     * @param tmCouponInfo 卡券信息表
     * @return cc.rengu.igas.momp.common.entity.TMCouponInfo 卡券信息表
     * @author liujinan
     * @since 2021/1/14 16:59
     */
    private TMCouponInfo mompSelectClaimableFreeCoupon(TMCouponInfo tmCouponInfo, String couponNo) throws Exception {

        CouponInfoMapper couponInfoMapper = new CouponInfoMapperImpl();
        return couponInfoMapper.mompSelectClaimableFreeCoupon(tmCouponInfo, couponNo);
    }

    /**
     * 查询营销活动用户信息表信息
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @param userId 用户ID
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2021/1/14 16:59
     */
    private List<TMMcUserInfo> checkUserInfo(String instId, String mcNo, String userId) throws Exception {

        //查询上送用户的卡bin信息
        BinInfoService binInfoService = new BinInfoServiceImpl();
        BinInfo binInfo = binInfoService.getCardBinInfo(userId);
        if (null != binInfo) {
            String binNo = binInfo.getBinNo();
            rglog.info("通过用户号<{}> 查询卡bin信息为 <{}>！", userId, binNo);
            userId = userId + "," + binNo;
        } else {
            rglog.info("通过用户号<{}> 查询卡bin信息为空！", userId);
        }

        McUserInfoMapper mcUserInfoMapper = new McUserInfoMapperImpl();

        return mcUserInfoMapper.selectMcUserInfoListByInstIdMcNoAcctNo(instId, mcNo, userId);
    }

    /**
     * 报文检查
     *
     * @param claimCouponOutChannelRequest 卡券领取接口请求对象
     * @author liujinan
     * @since 2021/1/14 16:59
     */
    private void messageValidation(ClaimCouponOutChannelRequest claimCouponOutChannelRequest) {

        /* 检查交易日期 */
        if (StringUtil.isNullorEmpty(claimCouponOutChannelRequest.getHeader().getTransDate())) {
            rglog.error("交易日期必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalTxnDate = claimCouponOutChannelRequest.getHeader().getTransDate();
        }

        /* 检查交易时间 */
        if (StringUtil.isNullorEmpty(claimCouponOutChannelRequest.getHeader().getTransTime())) {
            rglog.error("交易时间必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalTxnTime = claimCouponOutChannelRequest.getHeader().getTransTime();
        }

        /* 检查机构标识 */
        if (StringUtil.isNullorEmpty(claimCouponOutChannelRequest.getHeader().getInstId())) {
            rglog.error("机构标识必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalInstId = claimCouponOutChannelRequest.getHeader().getInstId();
        }

        /* 检查交易流水号 */
        if (StringUtil.isNullorEmpty(claimCouponOutChannelRequest.getHeader().getTraceNo())) {
            rglog.error("交易流水号必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 营销活动编号 */
        if (StringUtil.isNullorEmpty(claimCouponOutChannelRequest.getMcNo())) {
            rglog.error("领取卡券营销活动编号必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalMcNo = claimCouponOutChannelRequest.getMcNo();
        }

        /* 规则编号 */
        if (StringUtil.isNullorEmpty(claimCouponOutChannelRequest.getRuleIndex())) {
            rglog.error("领取卡券营销活动规则编号必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalRuleIndex = claimCouponOutChannelRequest.getRuleIndex();
        }

        /* 领取人 */
        if (StringUtil.isNullorEmpty(claimCouponOutChannelRequest.getClaimUser())) {
            rglog.error("领取卡券领取人必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalClaimUser = claimCouponOutChannelRequest.getClaimUser();
        }

        /* 卡券领取方式*/
        if (StringUtil.isNullorEmpty(claimCouponOutChannelRequest.getClaimType())) {
            rglog.error("卡券领取方式必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalClaimType = claimCouponOutChannelRequest.getClaimType();
        }

        /* 购买金额*/
        if (!StringUtil.isNullorEmpty(claimCouponOutChannelRequest.getPurchaseAmt())) {
            if (new BigDecimal(claimCouponOutChannelRequest.getPurchaseAmt()).compareTo(BigDecimal.ZERO) < 0) {
                throw new BizException(RespCodeEnum.FIELD_HAS_NEGATIVE_NUMBER.getRespCode(), RespCodeEnum.FIELD_HAS_NEGATIVE_NUMBER.getRespDesc());
            } else {
                globalPurchaseAmt = claimCouponOutChannelRequest.getPurchaseAmt();
            }
        }

        /* 锁定标志*/
        if (!StringUtil.isNullorEmpty(claimCouponOutChannelRequest.getCouponLockFlag())) {
            globalCouponLockFlag = claimCouponOutChannelRequest.getCouponLockFlag();
        }

        /* 检查渠道标识 */
        if (StringUtil.isNullorEmpty(claimCouponOutChannelRequest.getChannelId())) {
            rglog.error("渠道标识必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalChannelId = claimCouponOutChannelRequest.getChannelId();
        }

        globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
    }
}
